<?php
include('../conn.php');
/**
 * 自动发放积分返券
 */

class Auto_get_integral {

	public function auto() {
		$sql = "SELECT shou_huo_datetime, ordersid, shopid, userid, fan_juan_price FROM goodsorders WHERE fan_juan_price != 0.00 AND state = 5";
		$res = mysql_query($sql);

		$now_time = date('Y-m-d H:i:s');//当前时间
		
		while($row = mysql_fetch_array($res)) {
			$time = date('Y-m-d H:i:s',strtotime("+7 day",strtotime($row['shou_huo_datetime'])));//确认收货后七天
			if($now_time > $time) {
				//查询商户当前返券金额与厂商当前积分返券金池金额
				$sql  = "SELECT return_volume FROM user WHERE userid = " . $row['userid'];
				$sql .=	" UNION ";
				$sql .= "SELECT balance FROM shop WHERE shopid = " . $row['shopid'];
				$price = mysql_fetch_assoc(mysql_query($sql));
				//end
				
				//增加商户当前积分返券金额
				$return_volume = $price['return_volume']+$row['fan_juan_price'];//商户当前积分返券金额+订单积分返券金额
				$sql = "UPDATE user SET return_volume = $return_volume WHERE userid = " . $row['userid'];
				$res = mysql_query($sql);
				//end

				//扣除厂商当前积分返券金池金额
				$balance = $price['balance']-$row['fan_juan_price'];//厂商当前积分返券金池-订单积分返券金额
				$sql = "UPDATE shop SET balance = $balance WHERE shopid " . $row['shopid'];
				mysql_query($sql); 
				//end

				//该订单积分返券金额设置为0.00
				$sql = "UPDATE goodsorders SET fan_juan_price = 0.00 WHERE ordersid = " . $row['ordersid'];
				mysql_query($sql);
				//end
			}
		}
	}
}

$get_integral = new Auto_get_integral;
$get_integral->auto();
